mui.init()
mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	var uid = self.parameter;
	console.log(uid)

	var passreminder = new Vue({
		el: '.main',
		data: {
			list: "",
			time: "",
			subject: '',
			countdown: "",
			topicnum: ""
		},
		updated: function() {

		}
	})
	common.ajax("student/question/queryBreakThrough", {
		uid: common.uid,
		token: common.token,
		bookUid: uid
	}, function(data) {
		if(data.code == 200) {
			passreminder.list = data.data
			passreminder.time = data.data.totalTime / 60
		} else {

			$(".next").css({
				"pointer-events": "none",
				"background": "#ccc",
				"border-color": "#ccc"
			})
			mui.toast(data.message)
			return common.open('../Taskdetails/Taskdetails.html')
			return common.open('../Taskdetails/Taskdetails.html')
		}

	}, "get")

	$(".back").on("tap", function() {
		mui.back()
	})

	var temp = 1
	var switc = false
	$(".next").on("tap", function() {
		common.ajax("student/question/queryBreakThroughQuestion", {
			uid: common.uid,
			token: common.token,
			version: $(".main").attr("version"),
			bookUid: uid,
			index: temp
		}, function(data) {
			if(data.code == 200) {
				passreminder.topicnum = temp
				/*passreminder.subject = data.data*/
				template(data.data)
				var time = passreminder.list.totalTime / 60 - 1
				var second = 60

				down()

				function down() {
					if(second <= 0) {
						time--
						if(time < 0) {
							submits()
							return
						}
						second = 60
					} else {
						second--
					}
					passreminder.countdown = time + ':' + second
					setTimeout(down, 1000)
				}

				$(".remind").hide()
			} else {
				common.open('../Taskdetails/Taskdetails.html')
				mui.toast(data.message)
			}
		}, "get")
	})

	$(".tofollow").on("tap", function() {
		if(temp < passreminder.list.totalQuestion) {
			saveStudentAnswer(true)
		}
	})
	$(".question").on("tap", function() {
		if(temp > 1) {
			saveStudentAnswer(false)
		}
	})

	$(".submit").on("tap", function() {

		if(switc) {
			return
		}

		var studentAnswers = ''
		$.each($(".selected"), function(e) {
			studentAnswers += $(".selected").eq(e).attr("xuanze") + ","
		});

		var studentAnswers = studentAnswers.slice(0, studentAnswers.length - 1);

		common.ajax("student/question/saveStudentAnswer", {
			uid: common.uid,
			token: common.token,
			version: $(".main").attr("version"),
			bookUid: uid,
			questionUid: $(".questionId").attr("questionuid"),
			index: temp,
			studentAnswers: studentAnswers
		}, function(data) {
			if(data.code == 200) {
				console.log(studentAnswers)
				submits()
				//			   alert(studentAnswers)//选择ABCD
			} else {
				common.open('../Taskdetails/Taskdetails.html')
				mui.toast(data.message)
			}

		})
	})

	function subtract(temp) {
		common.ajax("student/question/queryBreakThroughQuestion", {
			uid: common.uid,
			token: common.token,
			version: $(".main").attr("version"),
			bookUid: uid,
			index: temp
		}, function(data) {
			if(data.code == 200) {
				passreminder.topicnum = temp
				template(data.data)
				//			passreminder.subject = data.data
			} else {
				/*common.open('../Taskdetails/Taskdetails.html')*/
				mui.back()
				mui.toast(data.message)
			}
		}, "get")
	}
	none()

	function none() {
		if(temp <= 1) {
			$(".question").hide()
		} else {
			$(".question").show()
		}
		if(temp >= passreminder.list.totalQuestion) {
			$(".tofollow").hide()
			$(".submit").show()
		} else {
			$(".tofollow").show()
			$(".submit").hide()
		}
	}

	function selected(type) {
		$(".choice").on("tap", function() {
			if(type == 1) {
				$(this).toggleClass("selected").siblings().removeClass("selected")
			} else {
				$(this).toggleClass("selected")
			}
		})
	}

	function template(subject) {
		var temp = ""
		if(subject.questionType == 1) {
			temp += '<div class="questionId type" questionId="' + subject.questionId + '" questionUid="' + subject.questionUid + '">单选题</div>'
		} else {
			temp += '<div class="questionId numtype" questionId="' + subject.questionId + '" questionUid="' + subject.questionUid + '">多选题</div>'
		}
		temp += '<div class="title">' + passreminder.topicnum + '.' + subject.questionContent + '</div>'
		for(var i = 0; i < subject.breakThroughAnswerVos.length; i++) {
			temp += '<div class="choice" xuanze ="' + subject.breakThroughAnswerVos[i].option + '">' + subject.breakThroughAnswerVos[i].option + '.' + subject.breakThroughAnswerVos[i].answerContent + '</div>'
		}
		$(".mui-content").html(temp)
		if(subject.userAnswers != null) {
			$.each(subject.userAnswers, function(e, a) {
				$.each($(".choice"), function(index) {
					if($(".choice").eq(index).attr("xuanze") == a) {
						$(".choice").eq(index).addClass("selected")
					}
				})
			});
		}
		selected(subject.questionType)
	}
	//保存答案
	function saveStudentAnswer(save) {
		if(switc) {
			return
		}

		switc = true
		var studentAnswers = ''
		$.each($(".selected"), function(e) {
			studentAnswers += $(".selected").eq(e).attr("xuanze") + ","
		});
		common.ajax("student/question/saveStudentAnswer", {
			uid: common.uid,
			token: common.token,
			version: $(".main").attr("version"),
			bookUid: uid,
			questionUid: $(".questionId").attr("questionuid"),
			index: temp,
			studentAnswers: studentAnswers.slice(0, studentAnswers.length - 1)
		}, function(data) {
			if(data.code == 200) {
				if(save) {
					++temp
				} else {
					--temp
				}
				switc = false
				subtract(temp)
				none()
			} else {
				mui.back()
				mui.toast(data.message)
			}

		})
	}

	function submits() {
		common.ajax("student/question/submit", {
			uid: common.uid,
			token: common.token,
			version: $(".main").attr("version"),
			bookUid: uid
		}, function(data) {
			if(data.code == 200) {
				common.ajax("student/question/queryBreakThroughResultVo", {
					uid: common.uid,
					token: common.token,
					bookUid: uid
				}, function(datas) {
					if(datas.code == 200) {
						$(".inventory").text(datas.data.totalQuestion)
						$(".success_num").text(datas.data.trueQuestion)
						$(".error_num").text(datas.data.trueQuestion)
						if(datas.data.isPass == 0) {
							$(".error").show()
						} else {
							$(".success").show()
						}
						var reading = plus.webview.currentWebview('../reading/TeacherProgram.html').opener();　　　　 //触发父页面的自定义事件(refresh),从而进行刷新
						mui.fire(reading, 'Taskdetails');
						var list = plus.webview.currentWebview('../Taskdetails/Taskdetails.html').opener();　　　　 //触发父页面的自定义事件(refresh),从而进行刷新
						mui.fire(list, 'shuaxin');
					} else {
						mui.back()
						mui.toast(datas.message)
					}
				}, "get")
			} else {
				mui.back()
				mui.toast(data.message)
			}
		})
	}

	$(".see").on("tap", function() {
		common.open('../Passthrough/View_results.html', '../Passthrough/View_results.html', uid)
	})
})

(document).on("tap", ".close", function() {
	common.open("../Taskdetails/Taskdetails.html")
	var load = plus.webview.getWebviewById("../Taskdetails/Taskdetails.html");
	mui.fire(load, "view");
})